package com.xfyr.mvpdemo.presenter;

import android.content.Context;

import com.orjl.networkj.BaseObserverObject;
import com.orjl.networkj.RxScheduler;
import com.xfyr.mvpdemo.base.BasePresenter;
import com.xfyr.mvpdemo.contract.MainContract;
import com.xfyr.mvpdemo.model.MainModelImpl;

import java.util.List;

import bean.NewsBean;

/**
 * 版权： 版权所有
 * <p>
 * 作者：suntinghui
 * <p>
 * 创建日期：2019/6/25 0025 16:13
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
public class MainPresenterImpl extends BasePresenter<MainContract.View> implements MainContract.Presenter {

    private MainContract.Model model;
    private Context mContext;

    public MainPresenterImpl(Context context) {
        model = new MainModelImpl();
        mContext = context;
    }


    @Override
    public void getNewsP(String key) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getNewsM(key, mView.newsCount())
                .compose(RxScheduler.Obs_io_main())
                .as(mView.bindAutoDispose())
                .subscribe(new BaseObserverObject<List<NewsBean>>(mContext) {
                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        mView.hideLoading();
                        mView.onSuccess(newsBeans);
                    }

                    @Override
                    public void onFailure(String code, String msg) {
                        super.onFailure(code, msg);
                        mView.hideLoading();
                        mView.onError(code, msg);
                    }
                });

    }

    @Override
    public void onRefresh() {
        mView.onListRefresh();
    }

    @Override
    public void onItemClick(NewsBean newsBean) {
        mView.onItemSelect(newsBean);
    }
}
